<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\BottomMenu;
use app\model\DiyPage;
use app\model\Config;

class BottommenuController extends Base
{

    public function list()
    {
        $mo = input('post.mo');
        if (empty($mo)) {
            $mo = 'bottom';
        }
        $pageid = input('post.pageid');
        if ($pageid == '-1'){
            $Bottommenutype = $this->userInfo['Bottommenutype'];
        }else{
            $diywhere['weid'] = weid();
            $diywhere['version'] = '2';
            if (!empty($pageid)) {
                $diywhere['id'] = $pageid;
            }
            $DiyPage = DiyPage::where($diywhere)->order('is_index desc')->find();
            if (!empty($DiyPage)) {
                $DiyPage = $DiyPage->toArray();
                $data['pagebase'] = iunserializer($DiyPage['pagebase'])[0];
                $Bottommenutype = $data['pagebase']['base']['Bottommenutype'];
            }
        }

        $where['weid'] = weid();
        $where['module'] = $mo;
        $where['tid'] = (int) $Bottommenutype;

        if (input('get.v') == Config::getconfig()['program_version']) {
            $where['is_submitaudit'] = 0;
        }

        $where['status'] = 1;
        $listdata = BottomMenu::where($where)
            ->order('sort asc')
            ->select()
            ->toArray();

        $config = Config::getconfig('pagestyle');
        
        if (!empty(trim($config['bottommenucolor']))) {
            $data['color'] = trim($config['bottommenucolor']);
        } else {
            $data['color'] = '#999';
        }

        if (!empty(trim($config['bottommenuselectedColor']))) {
            $data['selectedColor'] = trim($config['bottommenuselectedColor']);
        } else {
            $data['selectedColor'] = '#EB0909';
        }

        if (!empty(trim($config['bottommenubackgroundColor']))) {
            $data['backgroundColor'] = trim($config['bottommenubackgroundColor']);
        } else {
            $data['backgroundColor'] = '#fff';
        }

        $data['borderStyle'] = '#fff';

        $data['thisurl'] = input('post.thisurl');

        foreach ($listdata as $key => $vo) {
 
            $list[$key]['iconPath'] = toimg($vo['icon']);
            $list[$key]['selectedIconPath'] = toimg($vo['iconactive']);
            $list[$key]['hump'] = $vo['hump'];
            if ($vo['url'] == 'customurl') {
                $list[$key]['pagePath'] = $vo['customurl'];
            } else {
                $list[$key]['pagePath'] = $vo['url'];
            }
            $list[$key]['text'] = $vo['title'];
        }

        $data['list'] = $list;


        return $this->json(['data' => $data]);
    }
    public function getindex()
    {
        $where['weid'] = weid();
        $where['is_index'] = 1;
        $where['module'] = 'bottom';

        $data = BottomMenu::where($where)
            ->order('sort asc')
            ->find();
        if (empty($data)) {
            $data['url'] = '/pages/index/index';
        } else {
            $data = $data->toArray();
        }

        return $this->json(['data' => $data]);
    }
}
